package hy;

import org.apache.kafka.clients.producer.ProducerInterceptor;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

import java.util.Map;

public class MyInterCeptor implements ProducerInterceptor<String,String> {
    long sucNum;
    long errNum;
    //producer发送前会先调用该方法
    public ProducerRecord<String, String> onSend(ProducerRecord<String, String> r) {
        return new ProducerRecord<String,String>(r.topic(),r.partition(),r.timestamp(),r.key(),System.currentTimeMillis()+r.value(),r.headers());
    }

    //在kafka响应ack时会调用
    public void onAcknowledgement(RecordMetadata recordMetadata, Exception e) {
        if(e==null){
            sucNum++;
        }else{
            errNum++;
        }
    }
    //kfka的Producer关闭时会调用
    public void close() {
        System.out.println("本次共成功发送："+sucNum+",失败："+errNum);
    }
    //获取配置信息时调用
    public void configure(Map<String, ?> map) {
        System.out.println(map);
    }
}